#!/usr/bin/python
# -*- coding: utf-8 -*-
import maya.cmds as mc
import maya.mel as mel
import mayaPrint
import objectName
class ShapeName(mayaPrint.MayaPrint,objectName.Naming):
    def __init__(self):
        mayaPrint.MayaPrint.__init__(self)
    
    def getShapeOrig(self,inObj):
        #if obj has deform node
        shape0 = self.getShapeName(inObj)
        if shape0==None:
            self.mayaWarning( "%s no shape ,return < //"%inObj )
            return
        #diferent shape type different connect in connect out
        shapeType = mc.objectType(shape0)
        shapeConnectOut = ""
        shapeConnectInt = ""
        if shapeType=="mesh":
            shapeConnectInt = "inMesh"
            shapeConnectOut = "worldMesh"
        elif shapeType=="nurbsCurve" or shapeType=="nurbsSurface":
            shapeConnectInt = "create"
            shapeConnectOut = "worldSpace"
        
        allList = mc.ls( mc.listRelatives(inObj,shapes=True,c=True) ,type=shapeType )
        itsShapes = mc.listRelatives(inObj,shapes=1,pa=1)
        
        for sp in itsShapes:
            if sp not in allList:
                allList.append(sp)
        
        for obj in allList:
            value = mc.getAttr('%s.intermediateObject'%(obj))
            upTrans = mc.listRelatives(obj,parent=True,pa=True)
            hasOutPut = mc.listConnections("%s.%s"%(obj,shapeConnectOut),d=1)
            hasInPut = mc.listConnections( "%s.%s"%(obj,shapeConnectInt),s=1,d=0 )
            #self.mayaPrint(upTrans)
            if value and upTrans==[inObj] and hasOutPut!=None and hasInPut==None:
                #self.mayaPrint(obj)
                return obj,shapeConnectInt,shapeConnectOut
        
    def getShapeName(self,inputObj,isType=""):
        shapes = mc.listRelatives(inputObj,shapes=True,f=True)
        if shapes==None:
            return
        if isType=="":
            return shapes[0]
        elif mc.objectType(shapes[0],isType=isType):
            return shapes[0]
        else:
            raise TypeError( "Shape:%s is not type %s"%(shapes[0],isType) )
            
    def standardShapeName(self,mode="e"):
        "Standard Shape Name like: parent|parentShape"
        sels = mc.ls(sl=True)
        if sels==[]:
            allTrans = mc.ls(type="transform")
        else:
            allTrans = sels
        
        mc.select(cl=True)
        for trs in allTrans:
            itsShapes = mc.listRelatives(trs,shapes=1,pa=1)
            #no shape \next
            if itsShapes==None:
                continue
                
            lastName = self.getLastName( trs )
            shapeLastName = self.getLastName( itsShapes[0] )
            standardName = "%sShape"%( lastName )
            
            if shapeLastName!=standardName:
                if mode=="e":
                    try:
                        newShapeName = mc.rename( itsShapes[0],standardName )
                        mc.select(newShapeName,add=True)
                        self.mayaPrint( "rename %s %s;"%(itsShapes[0],standardName) )
                    except Exception,e:
                        #print itsShapes[0],e
                        pass
                else:
                    self.mayaWarning( "Not A Standard Shape Name <%s>"%(itsShapes[0]),True,command="from maya import cmds\ncmds.select('%s')"%(itsShapes[0]) )
        #print result
        if mode=="e":
            self.mayaPrint("Standard ShapeName ")
            
        return mc.ls(sl=True)
        
ShapeNameCls = ShapeName()